package jia.tools;


import java.io.File;
import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Sep 9, 2004
 * Time: 9:39:47 AM
 * Process:
 * **************************************************************************
 */
public class HumanNameGenerator {
    private static final Logger log = Logger.getLogger(HumanNameGenerator.class);
    //TODO:  Generate several different human word lists, and randomly choose between them
    private static File fileToBeParsed = new File("src/com/gryffonx/xml/humans.xml");
    private static String gender = "";
    private static SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;
    
    /**
     * @return
     */
    private static String getGender() {
        return gender;
    }
    
    /**
     * @param gender
     */
    private static void setGender(String gender) {
        HumanNameGenerator.gender = gender;
    }
    
    public static String generateName(String gender, int howMany) {
        if (howMany <= 0) {
            howMany = 1;
        }
        
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        setGender(gender);
        StringBuffer retVal = new StringBuffer();
        
        for (int i = 0; i < howMany; i++) {
            StringBuffer finishedName = new StringBuffer();
            
            try {
                finishedName.append(getPrefix(determineGroup()));
            } catch (Exception e) {
                if (GenericFlags.errorMessages){
                    log.error(e.getMessage());
                }
            }
            
            retVal.append(StringUtils.capitalize(finishedName.toString()));
            //  retVal.append("\n");
        }
        return retVal.toString();
    }
    
    public static String generateName() {
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        setGender(gender);
        StringBuffer retVal = new StringBuffer();
        StringBuffer finishedName = new StringBuffer();
        
        try {
            finishedName.append(getPrefix(determineGroup()));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        retVal.append(StringUtils.capitalize(finishedName.toString()));
        return retVal.toString();
    }
    
    
    private static String getPrefix(int group){
        String retVal = "";
        
        if (group == 1){
            retVal = generateGroup1();
        }else if (group == 2){
            retVal = generateGroup2();
        }else if (group == 3){
            retVal = generateGroup3();
        }else if (group == 4){
            retVal = generateGroup4();
        }else if (group == 5){
            retVal = generateGroup5();
        }else if (group == 6){
            retVal = generateGroup6();
        }
        return retVal;
    }
    
    /**
     * Figure out randomly how many syllables the name is going to have.
     * @return
     */
    private static int determineGroup(){
        return StaticDiceRoller.rolldSomething(6, 1);
    }
    
    /**
     * PREFIX1 + SUFFIX1
     * @return
     */
    private static String generateGroup1(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        }
        
        return retVal.toString();
    }
    
    /**
     * PREFIX1 + MIDDLE1
     * @return
     */
    private static String generateGroup3(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        }
        return retVal.toString();
    }
    
    /**
     * PREFIX1 + MIDDLE1 + SUFFIX1
     * i.e. ab + a + berial
     * @return
     */
    private static String generateGroup2(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        }
        return retVal.toString();
    }
    
    
    /**
     * MIDDLE1 + SUFFIX1
     * @return
     */
    private static String generateGroup4(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("middle", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        }
        
        
        return retVal.toString();
    }
    
    /**
     * MIDDLE1 + PREFIX
     * @return
     */
    private static String generateGroup5(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        }
        
        return retVal.toString();
    }
    
    /**
     * MIDDLE1 + PREFIX SUFFIX
     * @return
     */
    private static String generateGroup6(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("middle", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("prefix", xmlDoc, 1));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));
        if (retVal.toString().length() <= 4){
            retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        }
        return retVal.toString();
    }
    
}
